﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Brain.IO;
using System.IO;

namespace Brain.Editor
{
    public partial class PluginPacker : Form
    {
        public PluginPacker()
        {
            InitializeComponent();
        }

        private void btn_Search_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog open = new OpenFileDialog())
            {
                open.Multiselect = true;
                open.Filter = "Dynamic Link Library files|*.dll";

                if (open.ShowDialog() == DialogResult.OK)
                {
                    string[] m = open.FileNames;
                    this.list_DLLs.Items.AddRange(m);
                }
            }
        }

        private void button_Pack_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog save = new SaveFileDialog())
            {
                save.Filter = "Brain 2 Plugin Files|*.bpl";

                if (save.ShowDialog() == DialogResult.OK)
                {
                    FileInfo[] Files = new FileInfo[this.list_DLLs.Items.Count];
                    for (int i = 0; i < this.list_DLLs.Items.Count; i++)
                    {
                        string sel = (string)this.list_DLLs.Items[i];
                        Files[i] = new FileInfo(sel);
                    }

                    // Currently, Brain 2 Plugin does not have anything on it,
                    // it's just a binary file just like any other.
                    // The difference is that the Engine will unpack this binary
                    // on the assembly folder, so referenced DLLs will be found
                    // and loaded correctly
                    using (Stream s = save.OpenFile())
                    {
                        BinaryFile.Join(Files, s);
                    }
                }
            }
        }
    }
}
